package com.humngyang.com.hmy.controller;


import com.humngyang.com.hmy.entity.Role;
import com.humngyang.com.hmy.entity.RoleAuthority;
import com.humngyang.com.hmy.service.RoleAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/RoleAuthorityController")
public class RoleAuthorityController {

    @Autowired
    RoleAuthorityService roleAuthorityService;

    /**
     * Author : hmy
     *
     * @date 2020/2/26 14:45
     * describution : 查询所有的角色权限表
     **/

    @RequestMapping("/getAllRoleAuthority")
    public Map<String, Object> getAllRoleAuthority() {
        Map<String, Object> re = new HashMap<>();
        List<RoleAuthority> allAuthority = roleAuthorityService.getAllAuthority();
        re.put("flag", 1);
        re.put("list", allAuthority);
        return re;
    }

    @RequestMapping("/getTreeAuthorityByRole")
    public Map<String, Object> getTreeAuthorityByRole() {
        Map<String, Object> re = new HashMap<>();
        List<Map<String, Object>> treeAuthorityByRole = roleAuthorityService.getTreeAuthorityByRole();
        re.put("flag", 1);
        re.put("list", treeAuthorityByRole);
        return re;
    }

    /**
     * Author : hmy
     *
     * @date 2020/2/28 16:34
     * describution : 根据角色权限表主键id删除权限
     **/

    @RequestMapping("/deleteById")
    public Map<String, Object> deleteById(@RequestBody RoleAuthority roleAuthority) {
        Map<String, Object> re = new HashMap<>();
        Integer integer = roleAuthorityService.deleteById(roleAuthority);
        if (integer > 0) {
            re.put("flag", 1);
            re.put("message", "删除" + integer + "数据成功");
            return re;
        }
        re.put("flag", 0);
        re.put("message", "无效数据");
        return re;
    }

    /**
     * Author : hmy
     *
     * @date 2020/3/1 9:23
     * describution : 删除二级权限， 包括二级权限下的三级权限
     **/

    @RequestMapping("/secondDeleteById")
    public Map<String, Object> secondDeleteById(@RequestBody RoleAuthority roleAuthority) {
        Map<String, Object> re = new HashMap<>();
        Integer integer = roleAuthorityService.secondDeleteById(roleAuthority);

        if (integer > 0) {
            re.put("flag", 1);
            re.put("message", "删除" + integer + "条数据成功");
            return re;
        }
        re.put("flag", 0);
        re.put("message", "无效数据");
        return re;
    }
    
    /**
     * Author : hmy
     * @date 2020/3/1 10:39
     * describution :根据角色查找所有的权限树形结构
     **/
    @RequestMapping("/getTreeAuthirity")
    public Map<String, Object> getTreeAuthirity(@RequestBody Role role) {
        Map<String, Object> re = new HashMap<>();
        List<Map<String, Object>> roleList = roleAuthorityService.getRoleList(role);
        re.put("flag", 1);
        re.put("list", roleList);
        return re;
    }

    /**
     * Author : hmy
     * @date 2020/3/1 11:15
     * describution :根据角色权限的id集合删除对应的数据
     **/
    @RequestMapping("/deleteByIds")
    public Map<String, Object> deleteByIds(@RequestBody Map map) {
        Map<String, Object> re = new HashMap<>();
        List<String> list = (List<String>) map.get("id");
        if (CollectionUtils.isEmpty(list)){
            re.put("flag", 0);
            re.put("message", "无效的id");
            return re;
        }
        Integer integer = roleAuthorityService.deleteByIds(list);
        re.put("flag", 1);
        re.put("message", "删除"+integer+"条数据");
        return re;
    }


    /**
     * Author : hmy
     * @date 2020/3/2 17:53
     * describution : 添加权限设置
     **/

    @RequestMapping("/insertRoAu")
    public Map<String,Object> insertRoAu(@RequestBody Map<String ,Object> map){
        Map<String,Object> re = new HashMap<>();
        roleAuthorityService.addRoleAuth(map);
        re.put("flag",1);
        return re;
    }
}
